package com.ygxsk.carhome.ui.leads;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.listener.RxBus;
import com.ygxsk.carhome.response.LeadsFollowUpInfoBean;
import com.ygxsk.carhome.rxbus.RxBusFollowStep;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.views.MyLinearLayoutManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-07-12  10:41
 * @Description:跟进状态的布局；
 */
public class LeadsFollowUpPopu extends BottomPopupView {

    public LeadsFollowUpPopu(@NonNull Context context) {
        super(context);
    }

    //因为通用界面，标志从哪个页面过来；
    private int extra_jumpfrom_where;

    private Activity activity;
    private Context context;

    private ArrayList<LeadsFollowUpInfoBean> leads_followup_list_outSide = new ArrayList<>();

    //对于客户的跟进状态的适配器
    private LeadsFollowUpInfoAdapter leadsFollowUpInfoAdapter;

    //弹窗的标题
    private String titleName;

    //类似AlertView的弹窗
    private BasePopupView mAlertBasePopupView;

    public void setTitleName(String _titleName) {
        titleName = _titleName;
    }


    public LeadsFollowUpPopu(Activity _activity, @NonNull Context _context, int _extra_action, ArrayList<LeadsFollowUpInfoBean> _stationDatas) {
        super(_context);

        activity = _activity;
        context = _context;

        //不这样等于，直接 赋值 添加；
        //station_list_outSide = _stationDatas;
        extra_jumpfrom_where = _extra_action;

        if (_stationDatas != null && !_stationDatas.isEmpty()) {

            if (leads_followup_list_outSide != null && !leads_followup_list_outSide.isEmpty()) {
                leads_followup_list_outSide.clear();
            }

            if (leads_followup_list_outSide != null) {
                leads_followup_list_outSide.addAll(_stationDatas);
            }

        }

    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.populeadsfromwhere;
    }


    @Override
    protected void onCreate() {
        super.onCreate();

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onCreate--onCreate---onCreate--onCreate--onCreate");

        //取消按钮
        TextView btn_dialog_search_cancel = (TextView) this.findViewById(R.id.btn_dialog_search_cancel);
        //确定按钮
        TextView btn_dialog_search_decide = (TextView) this.findViewById(R.id.btn_dialog_search_decide);
        //中间的标题内容
        TextView titleNameWhereMiddle = (TextView) this.findViewById(R.id.titlenamewheremiddle);

        if (titleNameWhereMiddle != null) {
            titleNameWhereMiddle.setText(titleName);
        }

        //初始化适配器列表
        RecyclerView recyclerViewInStation = (RecyclerView) this.findViewById(R.id.recyclerviewinstationasset);
        MyLinearLayoutManager myLinearLayoutManager = new MyLinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerViewInStation.setLayoutManager(myLinearLayoutManager);

        //适配器的创建；页面适配器使用的是一个条目项布局
        leadsFollowUpInfoAdapter = new LeadsFollowUpInfoAdapter(R.layout.leadsfollowadapter, leads_followup_list_outSide);
        recyclerViewInStation.setAdapter(leadsFollowUpInfoAdapter);

        if (leadsFollowUpInfoAdapter != null) {
            leadsFollowUpInfoAdapter.notifyDataSetChanged();
        }

        //单独的点击条目
        leadsFollowUpInfoAdapter.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                //直接设置勾选；
                LeadsFollowUpInfoBean leadsFollowUpInfoBean = (LeadsFollowUpInfoBean) adapter.getItem(position);

                //如果该条目不可点击，那么给提示
//                boolean isCanCheck = leadsFollowUpInfoBean.isCanCheck();

//                if (isCanCheck) {

                if (leadsFollowUpInfoBean != null) {

                    //发送消息，给业务台账，填充选中的业务类型；
                    for (LeadsFollowUpInfoBean itemBottom : leads_followup_list_outSide) {

                        //是唯一的；
                        if (TextUtils.equals(leadsFollowUpInfoBean.getDictCode(), itemBottom.getDictCode())) {

                            if (itemBottom.isChecked()) {
                                itemBottom.setChecked(false);
                            } else {
                                itemBottom.setChecked(true);
                            }

                            LogUtils.json("itchen--选中的", itemBottom);

                        } else {
                            itemBottom.setChecked(false);
                        }

                    }

                    //是单选，直接刷新；
                    if (leadsFollowUpInfoAdapter != null) {
                        leadsFollowUpInfoAdapter.notifyDataSetChanged();
                    }

                }

//                } else {
//
////                    CommUtils.checkDialog(mAlertView);
////                    mAlertView = CommUtils.method_showAlertViewSingleSlowly(mActivityInstance, ConstantSign.ALERT_TITLE, getString(R.string.please_input_buinesstype), true);
////                    mAlertView.show();
//
//                    if (mAlertBasePopupView != null) {
//                        if (mAlertBasePopupView.isShow()) {
//                            mAlertBasePopupView.dismiss();
//                        }
//                        mAlertBasePopupView.onDestroy();
//                        mAlertBasePopupView = null;
//                    }
//
//                    mAlertBasePopupView = CommUtils.method_showBasePopupViewSingle(activity, context, getContext().getString(R.string.choose_other_status));
//
//                }
            }
        });

        //【取消】按钮
        if (btn_dialog_search_cancel != null) {
            btn_dialog_search_cancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        //【确定】按钮
        if (btn_dialog_search_decide != null) {
            btn_dialog_search_decide.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    //method_test();

                    //即将被传递的单个item;
                    LeadsFollowUpInfoBean leadsFollowUpInfoBean_upLoad = null;

                    if (leads_followup_list_outSide != null && !leads_followup_list_outSide.isEmpty()) {

                        //----------------------------------------------------------------------------------------
                        Iterator<LeadsFollowUpInfoBean> iterator = leads_followup_list_outSide.iterator();

                        if (iterator != null) {

                            while (iterator.hasNext()) {

                                LeadsFollowUpInfoBean item = iterator.next();

                                if (item != null) {

                                    if (item.isChecked()) {

                                        leadsFollowUpInfoBean_upLoad = item;

                                        LogUtils.json(ConstantSign.LOGCAT_NOMAL + "选中条目项是->", item);

                                        break;
                                    }

                                }
                            }
                        }
                        //----------------------------------------------------------------------------------------
                        if (leadsFollowUpInfoBean_upLoad != null) {

                            //标识是从哪个页面过来的；
                            RxBusFollowStep rxBusFollowStep = new RxBusFollowStep();
                            rxBusFollowStep.setActionFlag(ExtraValue.MODIFY_LEAD_FOLLOW_STEP_SELECT);
                            rxBusFollowStep.setLeadsFollowUpInfoBean(leadsFollowUpInfoBean_upLoad);
                            RxBus.getInstance().post(rxBusFollowStep);

                            //关闭该弹窗内容
                            dismiss();

                        } else {
                            //请选择 客户来源；
                            ToastUtils.showShort(R.string.select_lead_fromwhere);
                        }

                    } else {
                        //执行异常？
                        ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                    }

                }
            });
        }

    }

    @Override
    protected void onShow() {
        super.onShow();

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "界面弹窗 弹出");

    }


    @Override
    public void onDestroy() {
        //        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "弹窗--弹窗--弹窗onDestroy--onDestroy---onDestroy--onDestroy--onDestroy");
        if (leads_followup_list_outSide != null && !leads_followup_list_outSide.isEmpty()) {

            for (LeadsFollowUpInfoBean leadsFollowUpInfoBean : leads_followup_list_outSide) {
                leadsFollowUpInfoBean.setChecked(false);
            }

            if (leadsFollowUpInfoAdapter != null) {
                leadsFollowUpInfoAdapter.notifyDataSetChanged();
            }

        }

        super.onDestroy();

        CommUtils.checkBasePopupView(mAlertBasePopupView);

    }

    @Override
    protected boolean onBackPressed() {

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "拦截 弹出弹窗的  返回事件");

//        CommUtils.checkDialog(alertViewDialogSure);

//        if (mDatasResources!=null&&!mDatasResources.isEmpty()){
//        }

        return super.onBackPressed();
    }

    @Override
    protected int getMaxHeight() {
        //潜客的跟进状态改为半高度
//        return (int) (XPopupUtils.getScreenHeight(getContext()) * .6f);
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .5f);
    }

    /*
     * @Author:itchenqi175@163.com
     * @Time:2023-07-12  15:22
     * @Description:选择列表布局的适配器
     */
    //适配器内容：
    private class LeadsFollowUpInfoAdapter extends BaseQuickAdapter<LeadsFollowUpInfoBean, BaseViewHolder> {

        public LeadsFollowUpInfoAdapter(int layoutResId, @Nullable List<LeadsFollowUpInfoBean> data) {
            super(layoutResId, data);

            //如果设置蒙版的点击效果
//            addChildClickViewIds(R.id.linearLayoutMask);

        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, LeadsFollowUpInfoBean leadsFollowUpInfoBean) {

            if (leadsFollowUpInfoBean != null) {

                //是否被勾中
                boolean isChecked = leadsFollowUpInfoBean.isChecked();

                //2024年1月26日16:08:55 新新增逻辑，该状态之前的状态是不可以选中的，只能选中该状态靠后的状态。
                //是否可以被勾选中
//                boolean isCanCheck = leadsFollowUpInfoBean.isCanCheck();

                //填充名称：
                String typeName = leadsFollowUpInfoBean.getDictLabel();

                //外侧的蒙版
//                LinearLayout linearLayoutMask = (LinearLayout) baseViewHolder.getView(R.id.linearLayoutMask);

                //填充名称
                TextView textViewStationNameType = (TextView) baseViewHolder.getView(R.id.textviewstationinfonamelead);
                ImageView checkBoxInStationType = (ImageView) baseViewHolder.getView(R.id.checkboxinstationnamelead);

                //填充名称
                CommUtils.setText(textViewStationNameType, typeName);

//                if (isCanCheck) {
//                    linearLayoutMask.setVisibility(View.GONE);
//                } else {
//                    //不能被点击的逻辑，遮盖蒙版，并且点击蒙版给提示：
//                    linearLayoutMask.setVisibility(View.VISIBLE);
//                }


                //列表之中
                if (isChecked) {
                    //字体设置为选中
//                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.orangeygxsk));
                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.redcomm));
                    checkBoxInStationType.setVisibility(View.VISIBLE);

//                    checkBoxInStation.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            checkBoxInStation.setVisibility(View.VISIBLE);
//                        }
//                    });

                } else {

                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.dark));
                    checkBoxInStationType.setVisibility(INVISIBLE);

//                    checkBoxInStation.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            checkBoxInStation.setVisibility(GONE);
//                        }
//                    });

                }

            }

        }
    }

}
